package com.example.deliveryapplication

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.deliveryapplication.entity.BearingModel
import com.example.deliveryapplication.entity.BearingType
import com.example.deliveryapplication.sql.BearTypeDao
import com.example.deliveryapplication.sql.BearingModelDao

class BearingTypeViewModel : BaseViewModel() {
    private val mResult = MutableLiveData<List<BearingType>>()
    val result: LiveData<List<BearingType>> get() = mResult
    fun getAll(clientDao: BearTypeDao) {
        launch {
            val clients = clientDao.loadAllBearType()
            mResult.postValue(clients)
        }
    }

    private val mDeleteTypeResult = MutableLiveData<Int>()
    val deleteTypeResult: LiveData<Int> get() = mDeleteTypeResult
    fun deleteType(clientDao: BearTypeDao, typeId: String) {
        launch {
            Log.d("测试",typeId)
            val num=clientDao.getTypeWithModelCount(typeId).itemCount

            if (num==0) {
                val bean = clientDao.loadBearTypeById(typeId)
                val result = clientDao.deleteBearType(bean)
                mDeleteTypeResult.postValue(result)
            } else {
                mDeleteTypeResult.postValue(-1)

            }
//            val clients = clientDao.loadAllBearType()
//            mDeleteTypeResult.postValue(clients)
        }
    }

    private val mUpdateResult = MutableLiveData<Int>()
    val updateResult: LiveData<Int> get() = mUpdateResult
    fun updateType(typeDao: BearTypeDao, type: BearingType) {
        launch {
           val result=typeDao.updateBearType(type)
            mUpdateResult.postValue(result)
        }
    }
}